package com.yinp.proapp.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.yinp.proapp.R;
import com.yinp.proapp.base.fragment.AppBaseFragment;
import com.yinp.proapp.databinding.FragmentTodayUndeterminedBinding;
import com.yinp.proapp.module.task.fragment.ToDoCurFragment;
import com.yinp.proapp.module.task.fragment.ToDoHistoryFragment;
import com.yinp.proapp.utils.ColorFlipPagerTitleView;
import com.yinp.proapp.view.viewpager2.ViewPager2Utils;

import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: yinp
 * @Date: 2021/9/2
 * @Description:今日待做页面
 */
public class TodayUndeterminedFragment extends AppBaseFragment<FragmentTodayUndeterminedBinding> {

    public static TodayUndeterminedFragment getInstance() {
        TodayUndeterminedFragment todayUndeterminedFragment = new TodayUndeterminedFragment();
        Bundle bundle = new Bundle();
        todayUndeterminedFragment.setArguments(bundle);
        return todayUndeterminedFragment;
    }

    @Override
    protected void initViews(View view) {
        initIndicator();
    }

    public ToDoCurFragment toDoCurFragment;
    public ToDoHistoryFragment toDoHistoryFragment;
    private SparseArray<Fragment> fragments = new SparseArray<>();

    private void initIndicator() {
        toDoCurFragment = ToDoCurFragment.getInstance();
        toDoHistoryFragment = ToDoHistoryFragment.getInstance();
        fragments.put(0, toDoCurFragment);
        fragments.put(1, toDoHistoryFragment);
        bd.materialViewPager.setAdapter(ViewPager2Utils.getAdapter(TodayUndeterminedFragment.this, fragments));

        List<String> titleList = new ArrayList<>(Arrays.asList("当前", "历史"));
        bd.materialIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator7 = new CommonNavigator(getContext());
        commonNavigator7.setAdjustMode(true);
        commonNavigator7.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titleList == null ? 0 : titleList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context);
                simplePagerTitleView.setText(titleList.get(index));
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(getContext(), R.color._999));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(getContext(), R.color.ff4d4d));
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setOnClickListener(v -> bd.materialViewPager.setCurrentItem(index));
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 4));
                indicator.setLineWidth(UIUtil.dip2px(context, 56));
                indicator.setRoundRadius(UIUtil.dip2px(context, 2));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(ContextCompat.getColor(context, R.color.ff4d4d));
                return indicator;
            }
        });
        bd.materialIndicator.setNavigator(commonNavigator7);
        ViewPager2Utils.bind(bd.materialIndicator, bd.materialViewPager);
    }

    @Override
    public void onDestroyView() {
        ViewPager2Utils.unBind(bd.materialViewPager);
        super.onDestroyView();
    }
}
